package core.actor.player
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
		

	public class Dust extends Bitmap
	{
		private var _dustMC:MovieClip;
		private var _dust:Bitmap = new Bitmap();
		private var _totalFrames:Number;
		private var _currFrame:Number = 0;
		private var _templateBmd:BitmapData = new BitmapData(100,100,true,0x00000000);
		private static var _cache:Number = 0;
		
		public var _dustBM:Vector.<Bitmap> = new Vector.<Bitmap>();
		public var _dustBMD:Vector.<BitmapData> = new Vector.<BitmapData>();
		
		
		public function Dust(dustUrl:String):void
		{
			//this.addChild(_dust);
		
			if(_cache==1)
			{
				//_dustBMD = DustCache.cache;
				this.addEventListener(Event.ENTER_FRAME,update);
			}
			else
			{
				var dustMCLoader:Loader = new Loader();	
				dustMCLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,dustLoadHandler);
				dustMCLoader.load(new URLRequest(dustUrl));
			}
			
		}
		
		
		private function dustLoadHandler(e:Event):void
		{
			var dustMC:MovieClip = e.target.loader.content as MovieClip;
			dustMC.stop();
			var t:Matrix = new Matrix();		
			_totalFrames = dustMC.totalFrames;
			for(var i:int = 0;i<_totalFrames;i++)
			{
				dustMC.gotoAndStop(i+1);
				var tempBMD:BitmapData = _templateBmd.clone();
				t.identity();
				t.translate(50,50);
				tempBMD.draw(dustMC,t);
				_dustBMD.push(tempBMD);
				_dustBM.push(new Bitmap(tempBMD));
			}
			DustCache.cache = _dustBMD;
			//_totalFrames = DustCache.cache.length;
			_cache = 1;
			e.target.loader.removeEventListener(Event.COMPLETE,dustLoadHandler);
			this.addEventListener(Event.ENTER_FRAME,update);
		}
		
		private function update(e:Event):void
		{
			if(_currFrame<DustCache.cache.length)
			{
				this.bitmapData = DustCache.cache[_currFrame];
				_currFrame++;
			}
			else
			{	
				this.removeEventListener(Event.ENTER_FRAME,update);
				stage.removeChild(this);						
				delete(this);
				
			}
		}
		
		public function setDust(url:String):void
		{
			
		}
		
		//public function 
	}
}